package cn.kgc.springday04.factory;

import cn.kgc.springday04.anotation.MyComponent;

import java.lang.reflect.Constructor;

/**
 * @author 课工场
 * @date 2023/10/9
 * @description
 */
public class BeanFactory {


    public static  <T> T  getBean(Class<T> clazz){

        //  判定类上是否含有@Component注解
        MyComponent annotation = clazz.getAnnotation(MyComponent.class);
        if (annotation!=null){
            try {
                Constructor<T> declaredConstructor = clazz.getDeclaredConstructor();
                T t = declaredConstructor.newInstance();
                return  t;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            throw  new RuntimeException("没有@Component注解，无法创建对象");
        }
    }

}
